package com.oceansoft.core.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.hui.platform.system.Constants;
import com.hui.platform.system.utility.StringUtil;
import com.oceansoft.core.common.util.RequestUtils;
import com.oceansoft.core.common.constant.Constant;
import com.oceansoft.core.common.constant.ConstantMap;
import com.oceansoft.core.common.util.Pagination;
import com.oceansoft.core.domain.Dict;
import com.oceansoft.core.domain.SysOperLog;
import com.oceansoft.core.service.IDictionaryService;

/**
 * 数据字典维护
 * 
 * @author yuchao
 * 
 */
@Controller
@RequestMapping("/dictionary")
public class DictionaryController {

	@Resource
	private IDictionaryService dictionaryService;

	/**
	 * 字典值列表
	 * 
	 * @return
	 */
	@RequestMapping("/value_mgr/{pageIndex}.html")
	public String valueMgrList(ModelMap model, Dict dict,
			@PathVariable("pageIndex") int pageIndex) {
//		Pagination<Dict> page = dictionaryService.findByPage(pageIndex, dict);
//		model.addAttribute("page", page);
		return "/dictionary/value_mgr";
	}
	
	@RequestMapping("/value_mgr/list.json")
	public ModelAndView list(ModelMap model, HttpServletRequest request) {
		ModelAndView modelAndView = new ModelAndView(Constants.RESPONSE_VIEW);
		String pageIndex = request.getParameter("page");
		String dbId = request.getParameter("dbId");
		if(StringUtil.isNull(pageIndex))
		{
			pageIndex = "1";
		}
		
		Dict dict = (Dict)RequestUtils.getEntity(request, Dict.class);
		if(StringUtil.isNotNullAndBlank(dbId))
		{
			dict.setDbId(Integer.parseInt(dbId));
		}
		//String name = request.getParameter("name");
		Pagination<Dict> page = dictionaryService.findByPage(Integer.parseInt(pageIndex), dict);
		
		modelAndView.addObject("rows",page.getList());
		modelAndView.addObject("total",page.getTotalRows());
		return modelAndView;
	}

	/**
	 * 跳转到字典值新增
	 * 
	 * @return
	 */
	@RequestMapping("/gotoadd.html")
	public String gotoAddvalue(ModelMap model) {
		return "/dictionary/value_add";
	}

	@RequestMapping("/getname")
	public void getDictName(ModelMap model) {
		List<Dict> dict = dictionaryService.findName();
		model.addAttribute("list", dict); 
		//String jsonStr = JsonUtil.toJSON(dict).toString();
		//System.out.println(jsonStr);
		//model.addObject(jsonStr);
//		try {
//			response.getWriter().write(jsonStr);
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
	}

	@RequestMapping("/getvalue/{dbId}")
	public void getDictName(ModelMap model, @PathVariable("dbId") long dbId) {
		List<Dict> dict = dictionaryService.findValues(dbId);
		model.addAttribute("list", dict);
	}

	//lb参数表示是 入库,出库,在库
	@RequestMapping("/getWpztValue/{lb}")
	public void getWpztName(ModelMap model,@PathVariable("lb") String lb) {
		String dict_type="物品状态";
		List<Dict> dict = dictionaryService.findSysDictValues(lb,dict_type);
		model.addAttribute("list", dict);
	}

	//储物类型获取
	@RequestMapping("/getCwlxValue")
	public void getCwlxName(ModelMap model) {
		String dict_type="储物类型";
		List<Dict> dict = dictionaryService.findSysDictValues(dict_type);
		model.addAttribute("list", dict);
	}
	
	/**
	 * 加载表彰类别值
	 * 
	 * @param model
	 * @param dbId
	 */
	@RequestMapping("/getvalue/{dbId}/{jm}")
	public void getBzlbDictName(ModelMap model,
			@PathVariable("dbId") long dbId, @PathVariable("jm") long jm) {
		List<Dict> dict = dictionaryService.findBzlbValues(dbId, jm);
		model.addAttribute("list", dict);
	}

	/**
	 * 保存字典值新增
	 * 
	 * @return
	 */
	@RequestMapping("/add")
	public void saveAddvalue(ModelMap model, Dict dict) {
		if(StringUtil.isNullOrBlank(dict.getCode())){
			String code =  dictionaryService.getMaxCode(dict.getParentId());
			dict.setCode(code);
		}
		int result = dictionaryService.saveAddDict(dict);
		model.addAttribute(Constant.LOG_RECORD, new SysOperLog(Constant.CREATE,
				"新增", ConstantMap.MODUAL_SYS_MGR, "新增数据字典"));
		model.addAttribute("result", result);
	}

	/**
	 * 跳转到字典值编辑
	 * 
	 * @return
	 */
	@RequestMapping("/gotoedit/{dbId}.html")
	public String gotoEditvalue(ModelMap model, @PathVariable("dbId") long dbId) {
		Dict dict = dictionaryService.findByDbId(dbId);
		model.addAttribute("dict", dict);
		return "/dictionary/value_edit";
	}

	/**
	 * 保存字典值编辑
	 * 
	 * @return
	 */
	@RequestMapping("/edit")
	public void saveEditvalue(ModelMap model, Dict dict) {
//		System.out.println(dict.getName()+ dict.getRemark()+ dict.getOrderNum()+dict.getParentId()+ 
//				dict.getParentName()+ dict.getDbId());
		int result = dictionaryService.saveEdit(dict);
		model.addAttribute("result", result);
		model.addAttribute(Constant.LOG_RECORD, new SysOperLog(Constant.UPDATE,
				"编辑", ConstantMap.MODUAL_SYS_MGR, "编辑数据字典"));
	}

	/**
	 * 字典值删除
	 * 
	 * @return
	 */
	@RequestMapping("/delete/{dbId}")
	public void deletevalue(@PathVariable("dbId") long dbId, ModelMap model) {
		dictionaryService.saveDelete(dbId);
		model.addAttribute(Constant.LOG_RECORD, new SysOperLog(Constant.DELETE,
				"删除", ConstantMap.MODUAL_SYS_MGR, "删除数据字典"));
	}

	/**
	 * 获取字典值
	 * 
	 * @param model
	 * @param dbId表示字典表的parentid
	 * @param type对应方法流
	 */
	@RequestMapping("/getdictvalue/{dbId}/{type}")
	public void getDictValue(ModelMap model, @PathVariable("dbId") long dbId,
			@PathVariable("type") int type) {
		List<Dict> dict = dictionaryService.findDictValues(dbId, type);
		model.addAttribute("list", dict);
	}
	
	
	/**
	 * 检查输入code是否合法
	 * 
	 * @param model
	 * @param parentid表示字典表的parentid
	 * @param code
	 */
	@RequestMapping("/checkcode/{parentid}/{code}")
	public void CheckCode(ModelMap model, @PathVariable("parentid") long parentid,
			@PathVariable("code") String code) {
		boolean result = dictionaryService.checkcode(parentid, code);
		model.addAttribute("result", result);
	}
}
